package com.gujia.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by GuJia on 2017/3/4.
 */

//@Controller
public class IndexController {


    @RequestMapping(path={"/","/index"},method = {RequestMethod.GET})
    @ResponseBody
    public String index(){
        return "Hello GuJia";
    }


    @RequestMapping(path={"/profile/{groupId}/{userId}"})
    @ResponseBody
    public String profile(@PathVariable("userId") int userId,
                          @PathVariable("groupId") String groupId,
                          @RequestParam(value = "type",defaultValue = "1") int type,
                          @RequestParam(value = "key",defaultValue = "zz",required = false) String key){
        return String.format("Profile Page of %s %d, type: %d key:%s",groupId,userId,type,key);
    }


    @RequestMapping(path={"/vm"})
    public String template(Model model){

        model.addAttribute("value","value1");
        List<String> colors = Arrays.asList(new String[]{"RED","GREEN","BLUE"});
        model.addAttribute("colors",colors);

        Map<String,String> map = new HashMap<>();
        map.put("1","$1");
        map.put("2","$2");

        model.addAttribute("map",map);
        return "home";
    }

    @RequestMapping(path={"/request"})
    @ResponseBody
    public String template(Model model, HttpServletResponse response, HttpServletRequest request, HttpSession httpSession){

        StringBuilder sb = new StringBuilder();

        Enumeration<String> headerNames = request.getHeaderNames();
        while(headerNames.hasMoreElements()){
            sb.append(headerNames.nextElement());
        }
        sb.append(request.getQueryString()+"</br>");
        sb.append(request.getPathInfo()+"</br>");
        sb.append(request.getRequestURI()+"</br>");
        return sb.toString();
    }
}
